package com.eeepay.eeepay_v2.activity;


import android.app.ActionBar;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.BaseGrpcTask;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.view.CustomChooseView;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ProductApiServiceGrpc;
import cn.eeepay.api.grpc.nano.ProductSerProto;
import cn.eeepay.api.grpc.nano.ReqBaseProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/14 0014.
 * 代理产品
 */
public class AgentProductActivity extends ABBaseActivity{

//    private String[] products = {"超级快", "超级刷", "商通", "小宝"};
    private List<String> products = new ArrayList<>(); //业务产品名称
    private TitleBar titleBar;
    private LinearLayout lLayout_product;

    private String agentId;//代理商Id
    private GrpcTask grpcTask;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agent_product;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
//        titleBar.setShowRight(View.VISIBLE);
//        titleBar.setRightTextView("编辑");
//        titleBar.setRightTextColor(R.color.blue);

        lLayout_product = getViewById(R.id.layout_product);

        if(bundle != null){
            agentId = bundle.getString(Constant.AGENT_ID);
            LogUtils.d("TAG", "agent_id = " + agentId);
        }

        grpcTask = new GrpcTask();
        sendHttpRequest(0);
    }

    private void initProduct() {
        for (int i = 0; i < products.size(); i++){
            CustomChooseView cView = new CustomChooseView(mContext, null);
            cView.setTitle(products.get(i));
            ViewGroup.LayoutParams params = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            cView.setLayoutParams(params);
            cView.setCanChoose(false);
            lLayout_product.addView(cView);
        }
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        super.sendHttpRequest(taskId);
        grpcTask.execute(Constant.URL.URL, Constant.URL.PORT);
    }

    @Override
    protected void eventOnClick() {
//        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
//            @Override
//            public void onRightClick(View view) {
//                if("保存".equals(((TextView) view).getText())){
//
//                }else{
//                    for (int i = 0; i < lLayout_product.getChildCount(); i++){
//                        CustomChooseView cView = (CustomChooseView) lLayout_product.getChildAt(i);
//                        cView.setCanChoose(true);
//                        titleBar.setRightTextView("保存");
//                    }
//                }
//            }
//        });
    }

    private class GrpcTask extends BaseGrpcTask<ProductSerProto.AgentProductListApi>{

        @Override
        protected ProductSerProto.AgentProductListApi doInBack(ManagedChannel channel) {
            ProductApiServiceGrpc.ProductApiServiceBlockingStub stub = ProductApiServiceGrpc
                    .newBlockingStub(channel)
                    .withDeadlineAfter(Constant.grpc_time_out, TimeUnit.SECONDS);
            ReqBaseProto.ReqOneStr request = new ReqBaseProto.ReqOneStr();
            request.param = agentId;
            ProductSerProto.AgentProductListApi response = stub.getAgentProductList(request);
            return response;
        }

        @Override
        protected void updateUI(ProductSerProto.AgentProductListApi result) {
            if(result != null){
                if(result.resStatus.status){
                    ProductSerProto.AgentProductsApi[] list = result.dpList;
                    for (int i = 0; i < list.length; i++){
                        products.add(list[i].dpName);
                    }

//                    LogUtils.d("products = " + products.toString());
                    initProduct();
                }else {
                    showToast(result.resStatus.msg);
                }
            }
        }
    }
}
